/*
 * RI_Timer.c
 *
 *  Created on: 15-08-2012
 *      Author: Mati
 */

#include "Timer.h"

extern xSemaphoreHandle xAdc2UartSem;

void RIT_Config()
{
	//initialize RIT timer
	LPC_SC->PCONP |= (1<<16);             //power up RIT clock
	LPC_SC->PCLKSEL1 |= (3<<26);          //run RIT clock by 12.5MHz (prescale by 8)
	LPC_RIT->RICOUNTER = 0;               //set counter to zero
	LPC_RIT->RICOMPVAL = 12499999;        //tick every second
	LPC_RIT->RICTRL |= (1<<1) | (1<<3);   //enable timer clear on match

	NVIC_SetPriority(RIT_IRQn, 2);
	//NVIC_EnableIRQ(RIT_IRQn);
}

void RIT_IRQHandler()
{
    //clear flag
    LPC_RIT->RICTRL |= (1<<0);

    portBASE_TYPE xUartWokenByTimer = pdFALSE; //nie chcemy od razu budzic zadania
    //printf("Przerwanie RIT\n\r");
    //while ( 1 != (LPC_ADC->ADSTAT & 0x20)>>5 );					//wait for finish conversion
    xSemaphoreGiveFromISR( xAdc2UartSem, &xUartWokenByTimer );
    //LED_Toggle(1<<3);


    if(xUartWokenByTimer == pdTRUE){
    	//UART_PrintStr("Semafor zwrocil TRUE\n");
    	portYIELD();
	}
}
